#!/bin/bash
# install bind9 and bind9-utils in ubuntu 24.04
# check if the server only have one ip,if not,you need change the IP var
IP="$(hostname -I | awk '{print $1}')"
# create domain
domain="home.xieziqiang"
# add a domain record
domain_name="dnstest"
domain_name_ip="10.1.1.20"
apt update && apt install -y bind9 bind9-utils

cat >>/etc/bind/named.conf.local<<EOF
zone "${domain}" {
        type master;
        file "/etc/bind/${domain}.zone";
};
EOF

cat > /etc/bind/${domain}.zone <<EOF
\$TTL   604800
@   IN	SOA	ns1.${domain}. admin.${domain}. (
			      $(date +%Y%m%d%H)		; Serial
			 604800		; Refresh
			  86400		; Retry
			2419200		; Expire
			 604800 )	; Negative Cache TTL
;
@   IN	NS	ns1.${domain}.

ns1 IN  A   ${IP}
${domain_name}  IN  A   ${domain_name_ip}
EOF

if ! named-checkconf;then
    echo "named config check no pass,please check configfile"
    exit 1
else
    echo "named config check is pass"
fi
rndc reload 
echo "you can check your domain name if work."

